package csce315.group10.androidapp;

import java.io.File;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import csce315.group10.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class ChangePasswordDialog extends Activity{
	/* private instance variables */
	public CustomApp appState;
	String name;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.changepassworddialog);
        appState = ((CustomApp)getApplicationContext());
    }
    
    /* hande when the ok button is pressed */
    public void okButtonHandler(View v){
    	EditText old_password = (EditText) findViewById(R.id.Old_password_text);
    	EditText new_password = (EditText) findViewById(R.id.New_password_text);
    	EditText new_password_confirmation = (EditText) findViewById(R.id.New_password_confirmation_text);
    	/* if the password matches the current user's set password */
    	if(appState.get_current_user().check_password(old_password.getText().toString())){
    		/* and the new password is the same as the confirmed password */
    		if(new_password.getText().toString().equals(new_password_confirmation.getText().toString())){
    			appState.get_current_user().setPassword(old_password.getText().toString(), new_password.getText().toString());
    		}else{
    			Toast.makeText(this, "Passwords did not match" , Toast.LENGTH_LONG).show();
    		}
    	}else{
    		Toast.makeText(this, "Password was not correct" , Toast.LENGTH_LONG).show();
    	}
    	finish();
    }
    
    public void onDestroy(){
    	Serializer serializer = new Persister();
        File result = new File("customappData.xml");
        try {
			serializer.write(appState, result);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
}